
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>如何使用 vue + typescript 编写页面(1) - 默默默默燃</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="一枚前端搬砖队队员的记录册,原文地址
起步使用 vue-cli 创建项目
vue create demo ，demo就是创建项目的名称提示选择预设，选择 Manually select features 回车确认选择types,"> 
    <meta name="author" content="张白告丶"> 
    <link rel="alternative" href="atom.xml" title="默默默默燃" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">默默默默燃</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://zhanghao-web.github.io">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">如何使用 vue + typescript 编写页面(1)</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/Js"><b>「
                    </b>JS<b> 」</b></a>
                
                July 15, 2019
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2019/07/15/Ts/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8-vue-typescript-%E7%BC%96%E5%86%99%E9%A1%B5%E9%9D%A2-1/" title="如何使用 vue + typescript 编写页面(1)" class="">如何使用 vue + typescript 编写页面(1)</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>Words count</i>
                    24k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>Reading time</i>
                    21 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>Read count</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/Ts/" rel="tag">Ts</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <p><a href="https://juejin.im/post/5c662a7de51d4562e71c4277#comment" target="_blank" rel="noopener">原文地址</a></p>
<h1 id="起步"><a href="#起步" class="headerlink" title="起步"></a>起步</h1><p>使用 vue-cli 创建项目</p>
<p><code>vue create demo</code> ，demo就是创建项目的名称<br>提示选择预设，选择 <code>Manually select features</code> 回车确认<br>选择<code>typescript</code> <code>vuex</code>  <code>router</code> <code>babel</code> <code>css pre-processors</code>，不使用<code>linter</code>，不选单元测试有需要另说<br>回车后按照需要选择合适的选项<br>选择完毕后回车等待资源准备</p>
<h2 id="编码开始"><a href="#编码开始" class="headerlink" title="编码开始"></a>编码开始</h2><p>熟悉几个vue的装饰器 <code>vue-property-decorator</code></p>
<p>以下的装饰器的功能和原js编写的功能相同/相似，可以参照官方文档类比解读。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; Vue, Component, Inject, Provide, Prop, Model, Watch, Emit, Mixins &#125; <span class="keyword">from</span> <span class="string">'vue-property-decorator'</span></span><br></pre></td></tr></table></figure>
<h3 id="1-Vue-实际上就是-Vue-本身-继承vue相关属性和函数"><a href="#1-Vue-实际上就是-Vue-本身-继承vue相关属性和函数" class="headerlink" title="1. Vue 实际上就是 Vue 本身,继承vue相关属性和函数"></a>1. Vue 实际上就是 Vue 本身,继承vue相关属性和函数</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; &#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-Component-声明成一个vue的组件实例，如果不使用，则不能得到一个vue组件"><a href="#2-Component-声明成一个vue的组件实例，如果不使用，则不能得到一个vue组件" class="headerlink" title="2. @Component 声明成一个vue的组件实例，如果不使用，则不能得到一个vue组件"></a>2. @Component 声明成一个vue的组件实例，如果不使用，则不能得到一个vue组件</h3><p>第一种方式，不需要定义额外内容:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; &#125;</span><br></pre></td></tr></table></figure>
<p>第二种方式，定义相关内容:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">@Component(&#123;</span><br><span class="line">    <span class="comment">/* 这里和js版本编写的 vue 组件内容相同，</span></span><br><span class="line"><span class="comment">     * 凡是不能在ts里面完成的都可以在这里完成 </span></span><br><span class="line"><span class="comment">     * 最终会被合并到一个实例中。</span></span><br><span class="line"><span class="comment">     * 在这里定义的内容，不会被语法器获取到，因此必须要同步在class中声明</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    data()&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">myname</span>:<span class="string">""</span>,<span class="attr">age</span>:<span class="number">18</span> &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span><br><span class="line">    private myname:string;</span><br><span class="line">    mounted()&#123;</span><br><span class="line">        <span class="keyword">this</span>.myname;    </span><br><span class="line">        <span class="keyword">this</span>.age;<span class="comment">// 语法器报错，当前类找不到age属性</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="3-Provide-向任意层级的子组件提供可访问的属性，默认为当前属性的名称，可以指定名称"><a href="#3-Provide-向任意层级的子组件提供可访问的属性，默认为当前属性的名称，可以指定名称" class="headerlink" title="3.@Provide 向任意层级的子组件提供可访问的属性，默认为当前属性的名称，可以指定名称"></a>3.@Provide 向任意层级的子组件提供可访问的属性，默认为当前属性的名称，可以指定名称</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ParentComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">    @Provide() private info!:string;</span><br><span class="line">    @Provide(<span class="string">"next"</span>) private infoNext!:string;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-Inject-获取父级由Provide提供的属性，默认为当前属性的名称，可以指定名称，多个父级提供相同名称属性时，获取最近父级的名称属性"><a href="#4-Inject-获取父级由Provide提供的属性，默认为当前属性的名称，可以指定名称，多个父级提供相同名称属性时，获取最近父级的名称属性" class="headerlink" title="4. @Inject 获取父级由Provide提供的属性，默认为当前属性的名称，可以指定名称，多个父级提供相同名称属性时，获取最近父级的名称属性"></a>4. @Inject 获取父级由Provide提供的属性，默认为当前属性的名称，可以指定名称，多个父级提供相同名称属性时，获取最近父级的名称属性</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">    @Inject() private info!:string;</span><br><span class="line">    @Inject(<span class="string">"next"</span>) private infoNext!:string;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="5-Prop-由标签属性注入，获取对应标签属性上值-可配置具体prop内容，参照js版本props内容"><a href="#5-Prop-由标签属性注入，获取对应标签属性上值-可配置具体prop内容，参照js版本props内容" class="headerlink" title="5. @Prop 由标签属性注入，获取对应标签属性上值,可配置具体prop内容，参照js版本props内容"></a>5. @Prop 由标签属性注入，获取对应标签属性上值,可配置具体prop内容，参照js版本props内容</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">   @Prop() age!:number;</span><br><span class="line">   @Prop(&#123;<span class="attr">default</span>:<span class="number">1</span>&#125;) sex!:number;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">MyComponent</span> <span class="attr">:age</span>=<span class="string">"16"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">"ts"</span>&gt;</span>  </span><br><span class="line"><span class="javascript"><span class="keyword">import</span> MyComponent <span class="keyword">from</span> <span class="string">'./MyComponent.vue'</span>;</span></span><br><span class="line"></span><br><span class="line">@Component(&#123;</span><br><span class="line">    components:&#123; MyComponent &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="actionscript"><span class="class"><span class="keyword">class</span> <span class="title">PComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; &#125;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h3 id="6-Model-是v-model的装饰器，当自定义组件想使用v-model时，可以使用这种方式，配合emit可以双向传递属性值"><a href="#6-Model-是v-model的装饰器，当自定义组件想使用v-model时，可以使用这种方式，配合emit可以双向传递属性值" class="headerlink" title="6. @Model 是v-model的装饰器，当自定义组件想使用v-model时，可以使用这种方式，配合emit可以双向传递属性值"></a>6. @Model 是v-model的装饰器，当自定义组件想使用v-model时，可以使用这种方式，配合emit可以双向传递属性值</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">:checked</span>=<span class="string">"checked"</span> @<span class="attr">change</span>=<span class="string">"changed"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">"ts"</span>&gt;</span></span><br><span class="line">@Component</span><br><span class="line"><span class="actionscript"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span></span><br><span class="line"><span class="actionscript">   @Model(<span class="string">"change"</span>) checked!:number;</span></span><br><span class="line"><span class="actionscript">   changed(event:any)&#123; <span class="comment">/* 这里是偷懒写的any，在实际项目中需要避免 */</span></span></span><br><span class="line"><span class="actionscript">       <span class="keyword">this</span>.$emit(<span class="string">"change"</span>,event.target.value)</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">MyComponent</span> <span class="attr">:age</span>=<span class="string">"16"</span> <span class="attr">v-model</span>=<span class="string">"mycheck"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">"ts"</span>&gt;</span> </span><br><span class="line"><span class="javascript"><span class="keyword">import</span> MyComponent <span class="keyword">from</span> <span class="string">'./MyComponent.vue'</span>;</span></span><br><span class="line"></span><br><span class="line">@Component(&#123;</span><br><span class="line">    components:&#123; MyComponent &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="actionscript"><span class="class"><span class="keyword">class</span> <span class="title">PComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">private</span> mycheck:boolean = <span class="literal">false</span>;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="7-Watch-观察某个属性更新"><a href="#7-Watch-观察某个属性更新" class="headerlink" title="7. @Watch 观察某个属性更新"></a>7. @Watch 观察某个属性更新</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">   @Prop() age!:number;</span><br><span class="line">   @Watch(<span class="string">"age"</span>)</span><br><span class="line">   ageChange(newVal:number,<span class="attr">oldVal</span>:number)&#123;</span><br><span class="line">       <span class="comment">/*age属性更新时，处理相关内容*/</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="8-Emit-this-emit-的装饰器-如果没有指定名称，默认使用函数名称。有返回值时，使用返回值，没有则使用"><a href="#8-Emit-this-emit-的装饰器-如果没有指定名称，默认使用函数名称。有返回值时，使用返回值，没有则使用" class="headerlink" title="8. @Emit this.$emit 的装饰器,如果没有指定名称，默认使用函数名称。有返回值时，使用返回值，没有则使用"></a>8. @Emit this.$emit 的装饰器,如果没有指定名称，默认使用函数名称。有返回值时，使用返回值，没有则使用</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">   private myname = <span class="string">""</span>;</span><br><span class="line">   </span><br><span class="line">   @Emit()</span><br><span class="line">   ageChangeA()&#123; <span class="comment">/* 仅发送 this.$emit('ageChangeA') */</span>   &#125;</span><br><span class="line">   </span><br><span class="line">   @Emit()</span><br><span class="line">   ageChangeB(age:number)&#123; <span class="comment">/* 发送 age  this.$emit('ageChangeB',age) */</span>   &#125;</span><br><span class="line">   </span><br><span class="line">   @Emit()</span><br><span class="line">   ageChangeC(age:number)&#123;  <span class="keyword">return</span> <span class="number">1</span> <span class="comment">/* this.$emit('ageChangeC',1) 发送return 结果*/</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="9-Mixins"><a href="#9-Mixins" class="headerlink" title="9. Mixins"></a>9. Mixins</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// MyMixin.ts</span></span><br><span class="line">@Component</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">MyMixin</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">   <span class="comment">/* 如果使用private 修饰，则两个相同的 私有属性混入时，会产生冲突 */</span></span><br><span class="line">   protected myname = <span class="string">"张三"</span>;</span><br><span class="line">   created()&#123;  <span class="comment">/* 混入对象有自己的 生命周期函数*/</span> &#125;</span><br><span class="line">   getMyName()&#123; <span class="built_in">console</span>.log(<span class="string">"张三混入"</span>) &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// OtherMixin.ts</span></span><br><span class="line">@Component</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">OtherMixin</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">   <span class="comment">/* 如果使用private 修饰，则两个相同的 私有属性混入时，会产生冲突 */</span></span><br><span class="line">   protected myname = <span class="string">"李四"</span>;</span><br><span class="line">   created()&#123;  <span class="comment">/* 混入对象有自己的 生命周期函数*/</span> &#125;</span><br><span class="line">   getMyName()&#123; <span class="built_in">console</span>.log(<span class="string">"李四混入"</span>) &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Mixins</span>(<span class="title">MyMixin</span>,<span class="title">OtherMixin</span>) </span>&#123; </span><br><span class="line">   private myname = <span class="string">""</span>; <span class="comment">/* 混入对象已经定义，这里产生属性冲突 */</span></span><br><span class="line">   mounted()&#123;</span><br><span class="line">       <span class="keyword">this</span>.getMyName() <span class="comment">// 李四混入</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="对于computed，使用-get-替代"><a href="#对于computed，使用-get-替代" class="headerlink" title="对于computed，使用 get 替代"></a>对于computed，使用 get 替代</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123; </span><br><span class="line">   private myname = <span class="string">""</span>; <span class="comment">/* 混入对象已经定义，这里产生属性冲突 */</span></span><br><span class="line">   <span class="keyword">get</span> upperName()&#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="string">"A"</span> + <span class="keyword">this</span>.myname</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>装饰器可以参照 <a href="https://link.juejin.im/?target=http%3A%2F%2Fnpm.taobao.org%2Fpackage%2Fvue-property-decorator">vue-property-decorator</a></strong></p>
<ul>
<li>没有filters,没有指令相关装饰器，有需要可以在@Component里面补充，或者可以直接定义函数调用计算返回值 在class里定义的属性即data属性，需要赋值初始值。</li>
</ul>
<h2 id="typescript-混用js"><a href="#typescript-混用js" class="headerlink" title="typescript 混用js"></a>typescript 混用js</h2><p>当我们引用了一个js编写的模块时会报错，这时候，如果不在引入的index文件里面添加.d.ts描述文件，那么这个模块就没法在语义上一致通过.</p>
<h3 id="通用兼容性解决方案"><a href="#通用兼容性解决方案" class="headerlink" title="通用兼容性解决方案"></a>通用兼容性解决方案</h3><p>如果这个模块是由npm下载的，并且有@types版本，可以直接使用<code>npm install @types/xx</code>下载</p>
<p>可惜,大部分都是没有编写声明文件的.但是又需要使用到这个模块时,应该怎么做.</p>
<p><strong>自定义描述文件</strong></p>
<p>在项目的【根目录】下定义【模块相同的名称】的描述文件A.d.ts,在描述文件内编写模块声明描述</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// A.d.ts</span></span><br><span class="line">declare <span class="built_in">module</span> <span class="string">'*'</span>;</span><br></pre></td></tr></table></figure>
<p>这样属于一劳永逸的描述,但是无法描述到具体模块内容,因此这种方式仅仅引入的组件没有任何操作时,比如引入的是第三方开发的vue组件,可以用这种方式偷懒.</p>
<p><strong>详细描述文件</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// A.d.ts</span></span><br><span class="line">declare <span class="built_in">module</span>  <span class="string">"A"</span> &#123;</span><br><span class="line">    <span class="comment">// 添加具体的描述内容</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>可以从这里查询到<a href="http://microsoft.github.io/TypeSearch/" target="_blank" rel="noopener">@type</a>包查询，已经被编写的文件声明可以直接下载</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d='undefined'
    >Comments</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>张白告丶</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/zhanghao-web" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">149 <p>Articles</p></a></li>
                    <li><a href="/categories">23 <p>Categories</p></a></li>
                    <li><a href="/tags">47 <p>Tags</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>Contents</h4>
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#起步"><span class="toc-number">1.</span> <span class="toc-text">起步</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#编码开始"><span class="toc-number">1.1.</span> <span class="toc-text">编码开始</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-Vue-实际上就是-Vue-本身-继承vue相关属性和函数"><span class="toc-number">1.1.1.</span> <span class="toc-text">1. Vue 实际上就是 Vue 本身,继承vue相关属性和函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-Component-声明成一个vue的组件实例，如果不使用，则不能得到一个vue组件"><span class="toc-number">1.1.2.</span> <span class="toc-text">2. @Component 声明成一个vue的组件实例，如果不使用，则不能得到一个vue组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-Provide-向任意层级的子组件提供可访问的属性，默认为当前属性的名称，可以指定名称"><span class="toc-number">1.1.3.</span> <span class="toc-text">3.@Provide 向任意层级的子组件提供可访问的属性，默认为当前属性的名称，可以指定名称</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-Inject-获取父级由Provide提供的属性，默认为当前属性的名称，可以指定名称，多个父级提供相同名称属性时，获取最近父级的名称属性"><span class="toc-number">1.1.4.</span> <span class="toc-text">4. @Inject 获取父级由Provide提供的属性，默认为当前属性的名称，可以指定名称，多个父级提供相同名称属性时，获取最近父级的名称属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-Prop-由标签属性注入，获取对应标签属性上值-可配置具体prop内容，参照js版本props内容"><span class="toc-number">1.1.5.</span> <span class="toc-text">5. @Prop 由标签属性注入，获取对应标签属性上值,可配置具体prop内容，参照js版本props内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-Model-是v-model的装饰器，当自定义组件想使用v-model时，可以使用这种方式，配合emit可以双向传递属性值"><span class="toc-number">1.1.6.</span> <span class="toc-text">6. @Model 是v-model的装饰器，当自定义组件想使用v-model时，可以使用这种方式，配合emit可以双向传递属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-Watch-观察某个属性更新"><span class="toc-number">1.1.7.</span> <span class="toc-text">7. @Watch 观察某个属性更新</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-Emit-this-emit-的装饰器-如果没有指定名称，默认使用函数名称。有返回值时，使用返回值，没有则使用"><span class="toc-number">1.1.8.</span> <span class="toc-text">8. @Emit this.$emit 的装饰器,如果没有指定名称，默认使用函数名称。有返回值时，使用返回值，没有则使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-Mixins"><span class="toc-number">1.1.9.</span> <span class="toc-text">9. Mixins</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#对于computed，使用-get-替代"><span class="toc-number">1.1.10.</span> <span class="toc-text">对于computed，使用 get 替代</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#typescript-混用js"><span class="toc-number">1.2.</span> <span class="toc-text">typescript 混用js</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#通用兼容性解决方案"><span class="toc-number">1.2.1.</span> <span class="toc-text">通用兼容性解决方案</span></a></li></ol></li></ol></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            张白告丶
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
